package com.Khan.SocialRescueSys.config;


import com.Khan.SocialRescueSys.pojo.User;
import com.Khan.SocialRescueSys.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Khan Liu
 * @date 2022/4/5 22:09
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;
    /**授权*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }
    /**认证*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        UsernamePasswordToken userToken = (UsernamePasswordToken)token;
        User user = userService.queryByUsername(userToken.getUsername());
        if(user == null){
            return null;
        }
        return new SimpleAuthenticationInfo("",user.getPassword(),"");
    }
}
